﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Canyala.Lagoon.Functional;

namespace Canyala.Lagoon.Models
{
    /// <summary>
    /// Provides a concrete data type for email adresses.
    /// </summary>
    public class EmailAddress
    {
        private string[] _parts;
        private string[] _domainParts;

        private EmailAddress(string[] parts)
        { 
            _parts = parts;

            if (_parts.Length == 2)
                _domainParts = _parts[1].Split('.');
            else
                _domainParts = Seq.Array<string>();
        }

        public static EmailAddress Parse(string text)
            { return new EmailAddress(text.Split('@')); }

        public static EmailAddress FromDomainAndUser(string domain, string user)
            { return new EmailAddress(Seq.Array(user, domain)); }

        public string User 
            { get { return _parts.Length > 0 ? _parts[0] : String.Empty; } }

        public string Domain 
            { get { return _parts.Length == 2 ? _parts[1] : String.Empty; } } 

        public string TopDomain
            { get { return _domainParts.Length >= 2 ? _domainParts[_domainParts.Length - 1] : String.Empty; } }
    }
}
